package com.example.a10928.weifriend.http;

import android.content.Context;
import android.support.annotation.NonNull;

import com.example.a10928.weifriend.AppScope;
import com.example.a10928.weifriend.http.api.Api;
import com.example.a10928.weifriend.http.api.BpService;
import com.example.a10928.weifriend.http.api.OtherService;
import com.example.a10928.weifriend.http.configuration.DataParamsInterceptor;
import com.example.a10928.weifriend.http.configuration.DateFieldConverterFactory;
import com.example.a10928.weifriend.http.configuration.ErrorProcessInterceptor;
import com.example.a10928.weifriend.http.configuration.GsonParamsConverterFactory;
import com.example.a10928.weifriend.http.configuration.InsertSignInterceptor;
import com.example.a10928.weifriend.http.configuration.LowerCaseFieldNamingStrategy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.bind.DateTypeAdapter;

import java.util.concurrent.TimeUnit;

import javax.inject.Named;

import dagger.Module;
import dagger.Provides;
import io.reactivex.schedulers.Schedulers;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by Chuang on 2018/2/24.
 * 请声明此类的作用，如无声明则留null
 */
@Module
public class AppModule {

    private static Context mContext;

    public AppModule(Context context) {
        mContext = context;
    }


    @AppScope
    @Provides
    Context providesContext() {return mContext;}


    @AppScope
    @Provides
    OkHttpClient providesOkHttpClient() {
        return new OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .readTimeout(30, TimeUnit.SECONDS)
            .retryOnConnectionFailure(true)
            .addInterceptor(new ErrorProcessInterceptor())
            .addInterceptor(new InsertSignInterceptor())
            .addInterceptor(new DataParamsInterceptor())
            .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
            .build();
    }


    @AppScope
    @Provides
    Gson providesGson() {
        return new GsonBuilder()
            .setFieldNamingStrategy(new LowerCaseFieldNamingStrategy())
            .registerTypeAdapterFactory(DateTypeAdapter.FACTORY)
            .create();
    }

    @AppScope
    @Provides
    BpService providesBpService(OkHttpClient okHttpClient, Gson gson) {
        return newBpServiceRetrofitBuilder(okHttpClient, gson)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
            .create(BpService.class);
    }

    @AppScope
    @Provides
    @Named(BpService.ASYNC)
    BpService provideAsyncBpService(OkHttpClient okHttpClient, Gson gson) {
        return newBpServiceRetrofitBuilder(okHttpClient, gson)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
            .build()
            .create(BpService.class);
    }

    @AppScope
    @Provides
    @Named(BpService.IO)
    BpService provideIoBpService(OkHttpClient okHttpClient, Gson gson) {
        return newBpServiceRetrofitBuilder(okHttpClient, gson)
            .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
            .build()
            .create(BpService.class);
    }


    @NonNull
    private Retrofit.Builder newBpServiceRetrofitBuilder(OkHttpClient okHttpClient, Gson gson) {
        return new Retrofit.Builder()
            .baseUrl(Api.BASE_URL)
            .addConverterFactory(ScalarsConverterFactory.create())
            .addConverterFactory(GsonConverterFactory.create(gson))
            .addConverterFactory(DateFieldConverterFactory.create())
            .addConverterFactory(GsonParamsConverterFactory.create(gson))
            .client(okHttpClient);
    }

    private static OtherService otherService;

    public static OtherService newKLingdRetrofitBuilder() {

        if (otherService == null) {
            OkHttpClient.Builder okHttp = new OkHttpClient.Builder()
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                .retryOnConnectionFailure(true);
            Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(okHttp.build())
                .build();
            return retrofit.create(OtherService.class);
        }
        return otherService;
    }


}
